package TCPClient;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;

import messangerTypes.MessageBase;

public class TCPReadWorker extends Thread
{
	private InputStream in;
	public boolean quit = false;
	private TCPClient client;
	
	public TCPReadWorker (TCPClient client, InputStream in)
	{
		this.in = in;
		this.client = client;
	}
	
	public void run ()
	{
		 //BufferedReader inFromServer = new BufferedReader(new InputStreamReader(this.in));
		ObjectInputStream inFromServer;
		try
		{
			inFromServer = new ObjectInputStream (this.in);
		} catch (IOException e1)
		{
			return;
		}
		 
		 while(!quit)
		 {
			 
			try
			{
				MessageBase message = (MessageBase) inFromServer.readObject();
				System.out.println(message.toString()+ "\n readWorker");
				client.processMessage(message);
			} catch (IOException e)
			{
				quit = true;
			} catch (ClassNotFoundException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 
		 }
		 
	}
}
